/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package updatetree;

import java.text.*;
import java.util.*;

/**
 *
 * @author mani
 */
public class S2D {

    private String form;
    private int BYear;
    private int BMonth;
    private int BDay;
    private int week;

    public S2D(String format) throws ParseException {
        GregorianCalendar gcc = new GregorianCalendar();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
        Date parse = simpleDateFormat.parse(format);
        gcc.setTime(parse);

        BYear = gcc.get(GregorianCalendar.YEAR);
        BMonth = gcc.get(GregorianCalendar.MONTH) + 1;
        BDay = gcc.get(GregorianCalendar.DAY_OF_MONTH);

        toFormat(BYear, BMonth, BDay);
        week = computeWeek(BYear, BMonth, BDay);

    }

    S2D(int BeginYear, int BeginMonth, int BeginDay) throws ParseException {
        BYear = BeginYear;
        BMonth = BeginMonth;
        BDay = BeginDay;
        toFormat(BeginYear, BeginMonth, BeginDay);
        week = computeWeek(BYear, BMonth, BDay);
    }

    Date getDate() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
        Date parse = simpleDateFormat.parse(form);
        return parse;
    }

    Date getToday() {
        Date now = new Date();
        return new Date(now.getTime());
    }

    String getTodayChinese() {
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH);
        int day = instance.get(Calendar.DAY_OF_MONTH);
        return year + "-" + (month + 1) + "-" + day;
    }

    private void toFormat(String format) {
        form = format;
    }

    private void toFormat(int BeginYear, int BeginMonth, int BeginDay) {
        form = BeginYear + "-" + String.format("%02d", BeginMonth) + "-" + String.format("%02d", BeginDay);
    }

    public void IncreaseDay() throws ParseException {
        int dayNumber = getDayMouth(BYear, BMonth);
        if (BDay + 1 > dayNumber) {
            IncreaseMonth();
            BDay = 1;
        } else {
            BDay++;
        }
        toFormat(BYear, BMonth, BDay);
        week = computeWeek(BYear, BMonth, BDay);

    }

    public int getDayMouth(int year, int mouth) {
        int num = 0;
        switch (mouth) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                num = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                num = 30;
                break;
            case 2:
                num = isLeapYear(year) ? 29 : 28;
                break;
            default:
                System.out.println("非法月份");
                break;
        }
        return num;
    }

    public boolean isLeapYear(int year) {
        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
            return true;
        } else {
            return false;
        }
    }

    private void IncreaseMonth() {
        if (BMonth + 1 > 12) {
            IncreaseYear();
            BMonth = 1;
        } else {
            BMonth++;
        }

    }

    private void IncreaseYear() {
        BYear++;
    }

    private int computeWeek(int BYear, int BMonth, int BDay) throws ParseException {

        GregorianCalendar grc = new GregorianCalendar();
        grc.setTime(getDate());
        return grc.get(GregorianCalendar.DAY_OF_WEEK) - 1;
    }

    int getWeek() {
        return week;
    }

    String getString() {
        return form;
    }

    boolean isWorkDay() {
        if (week > 0 && week < 6) {
            return true;
        }
        return false;
    }

    void IncreaseTwoDay() throws ParseException {
        IncreaseDay();
        IncreaseDay();
    }

    void DecreaseDay() throws ParseException {
        if (BDay > 1) {
            BDay--;

        } else {
            DecreaseMonth();
        }
        toFormat(BYear, BMonth, BDay);
        week = computeWeek(BYear, BMonth, BDay);
    }

    private void DecreaseMonth() {
        if (BMonth > 1) {
            BMonth--;

        } else {
            DecreaseYear();
        }

        BDay = getDayMouth(BYear, BMonth);
    }

    private void DecreaseYear() {
        BYear--;
        BMonth = 12;
    }

    boolean compareToday(String lastday, int updateday) throws ParseException {


        S2D lastdaydate = new S2D(lastday);
        S2D chineseday = new S2D(getTodayChinese());
        int daycount = 0;
        while (true) {
            if (chineseday.getDate().compareTo(lastdaydate.getDate()) == 0) {
                if (daycount >= updateday) {
                    return true;
                } else {
                    return false;
                }
            }
            lastdaydate.IncreaseDay();
            daycount++;
        }

    }
}
